package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zhilei.base.BaseInfoProperties;
import com.zhilei.exceptions.GraceException;
import com.zhilei.grace.result.ResponseStatusEnum;
import com.zhilei.mapper.DataDictionaryMapper;
import com.zhilei.pojo.DataDictionary;
import com.zhilei.pojo.bo.DataDictionaryBO;
import com.zhilei.pojo.vo.DataDictionaryVO;
import com.zhilei.service.DataDictionaryService;
import com.zhilei.untils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author 志磊
 * @since 2024-09-22
 */
@Service
public class DataDictionaryServiceImpl extends BaseInfoProperties implements DataDictionaryService {

    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    /**
     * 创建数据字典
     *
     * @param dataDictionaryBO
     */
    @Override
    @Transactional
    public void createDataDict(DataDictionaryBO dataDictionaryBO) {
        DataDictionary dataDictionary = new DataDictionary();

        if (dataDictionaryBO.getId() == null) {
            // 验证typeCode和typeName不能重复出现
            DataDictionary ddExits = dataDictionaryMapper.selectOne(new QueryWrapper<DataDictionary>()
                    .eq("item_key", dataDictionaryBO.getItemKey())
                    .eq("item_value", dataDictionaryBO.getItemKey()));
            if (ddExits != null)
                GraceException.disPlay(ResponseStatusEnum.DATA_DICT_EXIST_ERROR);
        }
        BeanUtils.copyProperties(dataDictionaryBO, dataDictionary);
        dataDictionaryMapper.insert(dataDictionary);
    }

    /**
     * 删除数据字典
     *
     * @param dictId
     */
    @Transactional
    @Override
    public void deleteDataDictById(String dictId) {
        dataDictionaryMapper.deleteById(dictId);
    }

    /**
     * 修改数据字典
     *
     * @param dataDictionaryBO
     */
    @Transactional
    @Override
    public void modifyDataDict(DataDictionaryBO dataDictionaryBO) {
        String id = dataDictionaryBO.getId();
        if (StringUtils.isBlank(id))
            GraceException.disPlay(ResponseStatusEnum.SYSTEM_ERROR_NOT_BLANK);
        DataDictionary dataDictionary = new DataDictionary();
        BeanUtils.copyProperties(dataDictionaryBO, dataDictionary);
        dataDictionaryMapper.updateById(dataDictionary);
    }

    /**
     * 分页查询数据字典可以模糊查询
     *
     * @param typeName
     * @param itemValue
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PagedGridResult getDataDictList(String typeName, String itemValue, Integer page, Integer limit) {

        PageHelper.startPage(page, limit);
        List<DataDictionary> dataDictionaries = dataDictionaryMapper.selectList(new QueryWrapper<DataDictionary>()
                .like("type_name", typeName)
                .like("item_value", itemValue));

        ArrayList<DataDictionaryVO> dataDictionaryVOList = new ArrayList<>();
        for (DataDictionary dataDictionary : dataDictionaries) {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            BeanUtils.copyProperties(dataDictionary, dataDictionaryVO);
            dataDictionaryVOList.add(dataDictionaryVO);
        }

        return setterPagedGrid(dataDictionaryVOList, page);
    }

    /**
     * 根据字典类别代码查询数据列表
     *
     * @param typeCode
     * @return
     */
    @Override
    public List<DataDictionaryVO> getDataByCode(String typeCode) {

        if (StringUtils.isBlank(typeCode))
            GraceException.disPlay(ResponseStatusEnum.USER_STATUS_ERROR);

        // 查询字典类别下的数据，只返回 enabled=1的
        List<DataDictionary> dataDictionaries =
                dataDictionaryMapper.selectList(new QueryWrapper<DataDictionary>()
                        .eq("type_code", typeCode)
                        .eq("enabled", 1));

        ArrayList<DataDictionaryVO> DataDictionaryVOList = new ArrayList<>();
        for (DataDictionary dataDictionary : dataDictionaries) {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            BeanUtils.copyProperties(dataDictionary, dataDictionaryVO);
            DataDictionaryVOList.add(dataDictionaryVO);
        }
        return DataDictionaryVOList;
    }

    /**
     * 查询单条数据
     *
     * @param dictId
     * @return
     */
    @Override
    public DataDictionaryVO getModifyDataDictById(String dictId) {
        DataDictionary dataDictionary = dataDictionaryMapper.selectOne(new QueryWrapper<DataDictionary>()
                .eq("id", dictId));
        DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
        BeanUtils.copyProperties(dataDictionary, dataDictionaryVO);
        return dataDictionaryVO;
    }

    /**
     * 查询数据字典的键
     *
     * @param keys
     * @return
     */
    @Override
    public List<DataDictionary> getItemsByKeys(String[] keys) {
        return dataDictionaryMapper.selectList(new QueryWrapper<DataDictionary>()
                .eq("enable", 1)
                .in("item_key", keys));
    }


}
